<?php
class Controller{
	private $base;
	private $keyword;
	private $arguments;
	
	private $templateHTML = array();
	
	
	function init() {
		F::get()->Controller($this);
		$this->startSession();
		
		$this->setup();
		
			//TODO update this to be more secure
		/* Handles Posting to Classes */
		if( isset( $_POST['action'] ) ){
			$actionArgs = explode('|',$_POST['action']);
			$className 		= $actionArgs[0];
			unset($actionArgs[0]);
			$classArgs = array_values((array)$actionArgs);
			$class = call_user_func_array(array(F::get(), $className), $classArgs );
			$class->post($_POST['actionSwitch']);
		}
		
		
		switch( $this->getKeyword() ){
			case 'form':{
				new Ajax(false);
				break;
			}
			case 'ajax':{
				new Ajax();
				break;
			}
			case 'api':{
				F::get()->API($_POST['apiKey'], $_POST['enc_req'], $this->getArg(0) )->_draw();
				break;
			}
			case 'update':{
				//TODO Remove and move into a trigger 
				F::get()->DB()->showErrors(false);
				new Updater();
				F::get()->DB()->showErrors(true);
				F::get()->Tpl()->redirect('home');
				break;
			}
			default:{
				$this->setBreadcrumbs();
				F::get()->Auth()->clearTokens();
				
				$page = F::get()->loader('Page', $this->getKeyword(), true, 'keyword');
				$page->draw();
				
				echo('
					<div class="modal fade hide" id="d-dialog">
					<div class="modal-header">
					<a class="close" data-dismiss="modal">&times;</a>
					<h3>Modal header</h3>
					</div>
					<div class="modal-body">
					<p>One fine body</p>
					</div>
					<div class="modal-footer">
					<a href="javascript:void(0);" class="btn btn-primary">Save changes</a>
					<a href="javascript:void(0);" class="btn modal-close-btn">Close</a>
					</div>
					</div>');
				//TODO Work on the cool editing dialog with json ajax server ? #! ? 
				$this->prepareTemplate();
			}
		}
	}
	
	private function startSession(){
		session_start();
		
		if(!F::get()->Auth()->isLoggedIn()){
			session_regenerate_id();
		}
	}
	
	private function setBreadcrumbs(){
		if( !isset($_SESSION['breadcrumbs'] )){
			$_SESSION['breadcrumbs'] = array();
		}
		array_unshift($_SESSION['breadcrumbs'],$this->getKeyword() );
		$_SESSION['breadcrumbs'] = array_unique($_SESSION['breadcrumbs']);
		$_SESSION['breadcrumbs'] = array_slice($_SESSION['breadcrumbs'], 0, 10);
	}
	
	
	/**
	* Assisgn the url arguments to correct class variable
	*/
	private function setup(){
		$argArray = explode('/', $_SERVER['PHP_SELF']);
		
		if( $argArray ){
			$this->base = $argArray[1];
			$this->keyword = $argArray[3];
			
			if( $this->keyword == '' ){
				F::get()->Tpl()->redirect('home');
			}
			
			unset( $argArray[0] );
			unset( $argArray[1] );
			unset( $argArray[2] );
			unset( $argArray[3] );
			
			$this->arguments = array_values( $argArray );
		}
		
	}
	
	function getKeyword(){
		return $this->keyword;
	}
	function getArg($argIndex){
		return $this->arguments[$argIndex];
	}
	
	/**
	* Loads the template into a array , so i can use the capture function within
	*/
	function prepareTemplate(){
		$this->templateHTML['top'] = F::get()->Tpl()->drawTop();
		$this->templateHTML['bot'] = F::get()->Tpl()->drawBottom();
	}
	
	function templateContent($buffer){
		$buffer = $this->templateHTML['top'].$buffer.$this->templateHTML['bot'];
		
		header('Content-Type: text/html; charset=UTF-8'); 
		
		//header("Content-Encoding: gzip");
		//$buffer = ob_gzhandler($buffer, 5);
		return $buffer;
	}
}
?>
